package com.ty.init.controller;

import com.ty.init.controller.ex.CaptchaNotMatchException;
import com.ty.init.pojo.User;
import com.ty.init.service.UserService;
import com.ty.init.service.impl.ex.PasswordNotMatchException;
import com.ty.init.service.impl.ex.UserNotFindException;
import com.ty.init.uitl.JsonResult;
import com.ty.init.uitl.Vo.UserVo;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.List;

/**
 * @author ty
 */
@RestController
@RequestMapping("users")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;
    @RequestMapping("login")
    public JsonResult<Object> Login (String username, HttpServletRequest request,String password, @RequestParam("captcha") String captcha, HttpSession session){
        if (!CaptchaUtil.ver(captcha, request)) {
            return JsonResult.fail("验证码不正确");
        }
        User data = userService.login(username, password);
        session.setAttribute("uid", data.getUid());
        session.setAttribute("username",data.getUsername());
        System.out.println(getUidFromSession(session));
        System.out.println(getUsernameFromSession(session));
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("userlist")
    public JsonResult<List<User>> findUsers(){
        List data = userService.findUsers();
        int count = data.size();
        return new JsonResult<>(OK ,count, data);
    }
    @RequestMapping("userlimit")
    public JsonResult<List<User>> findUsersLimit(Integer page,Integer limit){
        int start = (page-1)*limit;
        List usersLimit = userService.findUsersLimit(start,limit);
        List users = userService.findUsers();
        int count = users.size();
        return new JsonResult<>(OK,count,usersLimit);
    }
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }
    @RequestMapping("addUser")
    public JsonResult<Void> addUser(User user){
        userService.addUser(user);
        return new JsonResult<>(OK);
    }
    @RequestMapping("deleteUser")
    public JsonResult<Void> deleteUser(Integer uid){
        Integer result = userService.deleteUser(uid);
        if (result != 1){
            throw new UserNotFindException("用户不存在");
        }
        return new JsonResult<>(OK);
    }
    @RequestMapping("updateUserMessage")
    public JsonResult<Void> userUpdate(HttpSession session,User user){
        Integer integer = userService.userUpdate(user, getUsernameFromSession(session));
        System.out.println(integer);
        return new JsonResult<>(OK);
    }
    @RequestMapping("changePassword")
    public JsonResult<Void> changePassword(HttpSession session,String oldPassword,String newPassword){
        Integer uid = getUidFromSession(session);
        Integer integer = userService.changePassword(uid, oldPassword, newPassword);
        if (integer != 1){
            throw new PasswordNotMatchException("修改密码失败");
        }
        return new JsonResult<>(OK);
    }
    @RequestMapping("userMessage")
    public JsonResult<List<UserVo>> userMessage(){
        List<UserVo> result = userService.userMessage();
        return new JsonResult<>(OK, result);
    }
}
